from django.contrib.auth.backends import ModelBackend
from users.models import UserModel
from django.db.models import Q
def get_user_by_account(account):
    user = UserModel.objects.filter(Q(username=account) | Q(mobile=account) | Q(email=account)).first()
    return user
class CoustomJwtModelBackends(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        if username is None:
            username = kwargs.get(UserModel.USERNAME_FIELD)
        if username is None or password is None:
            return
        # try:
        user = get_user_by_account(username)

        # 条件查询出用户，根据用户判断密码是否正确，判断用户是否是活跃用户
        if user and user.check_password(password) and self.user_can_authenticate(user):
            return user



            # user = UserModel._default_manager.get_by_natural_key(username)

        # except UserModel.DoesNotExist:
            # Run the default password hasher once to reduce the timing
            # difference between an existing and a nonexistent user (#20760).
            # UserModel().set_password(password)
        # else:
        #     if user.check_password(password) and self.user_can_authenticate(user):
        #         return user